#include "fun.h"
#include "iic.h"

u8 bdata LED;
u8 level,ADC_count,t_100ms = 1,i,x1,x2,x3,x4; 
u8 time_flag,cnt = 1,led_count,index = 1,L_flag;
u16 s_count;

u8 code t_display[]={                       //标准字库
//   0    1    2    3    4    5    6    7    8    9    C     -
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x39,0x40,0x00};    

u8 T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};      //位码
u8 data_disp[][8] = 
{
    {11,0,11,12,0,0,0,0},
    {12,12,12,12,12,12,12,12},
    {12,12,12,12,12,12,11,0}
};

void Timer2Init(void)		//1毫秒@12.000MHz
{
	AUXR |= 0x04;		//定时器时钟1T模式
	T2L = 0x20;		//设置定时初值
	T2H = 0xD1;		//设置定时初值
	AUXR |= 0x10;		//定时器2开始计时
    IE2 |= 0x04;
    EA = 1; 
}

void smg_pro(void)
{
    if(ui_status)
    {
        index = 0;
        data_disp[0][1] = mode;
        data_disp[0][4] = (delay_t >= 10)?1:12;
        data_disp[0][5] = delay_t % 10;
        data_disp[0][6] = 0;
        data_disp[0][7] = 0;
    } 
    else if(!ui_status && level_flag)
    {
        index = 2;
        data_disp[index][7] = level;
        if(key_up)        
            level_flag = 0;
    }
    else
        index = 1;
}

void led_pro(void)
{
    if(mode == 1 && time_flag)
    {
        time_flag = 0;
        x1 %= 8;
        LED = 0x01 << x1;
		x1++;
    }
    else if(mode == 2 && time_flag)
    {
        time_flag = 0;
        x2 %= 8;
        LED = 0x80 >> x2;
		x2++;
    }
    else if(mode == 3 && time_flag)
    {
        time_flag = 0;
        x3 %= 4;
        LED = (0x01 << x3) | (0x80 >> x3);
		x3++;
    }
    else if(mode == 4 && time_flag)
    {
        time_flag = 0;
        x4 %= 4;
        LED = (0x08 >> x4) | (0x10 << x4);
		x4++;
	}
}

void main(void)
{
    buzzer(0);
    relay(0);
    led(LED);
    ADC_count = ADC_turn();     //读电位器Rb2控制的模拟电压
    delay_t = read_24C02(0x00);    //读取EEPROM存储的间隔时间
	if(delay_t > 12)
		delay_t = 12;
    // run_time = (run_time<<8) + read_24C02(0x00);
    Timer2Init();

    while(1)
    {
        if(update_flag == 2)
        {
            write_24C02(0x00,delay_t);
            update_flag = 0;
        }

        if(t_100ms)     //每100ms读取一次模拟电压
        {
            t_100ms = 0;
            ADC_count = ADC_turn();
            if(ADC_count >= 192)        //根据模拟电压值改变led亮度等级
            {
                level = 4;
                cnt = 7;
            }
            else if(ADC_count >= 128)
            {
                level = 3;
                cnt = 5;
            }
            else if(ADC_count >= 64)
            {
                level = 2;
                cnt = 3;
            }
            else
            {
                level = 1;
                cnt = 1;
            }
        }

        smg_pro();
		if(led_flag)
			led_pro();
		else
			LED = 0x00;
	}
}


void T2_callback(void) interrupt 12
{
    static u16 time = 0; 
    static u8 t = 0,key_count = 0;

    if(++key_count >= 10)
    {
        key_count = 0;
        key_scan();
    }
    if(++t >= 100)
    {
        t = 0; 
        t_100ms = 1;
    }

    if(ui_status)
    {
        if(++s_count >= 800)
        {
            s_count = 0;
            L_flag = ~L_flag;
        }
    }

    if(led_flag)
    {
        time++;
        if(time >= (delay_t * 100))     //每run_time毫秒led流动一次
        {
            time = 0;
            time_flag = 1;
        }

        if(led_count <= cnt)
            led(LED);           
        else
            led(0x00);
        
        if(++led_count == 8)
            led_count = 0;
    }
	else
		led(LED);

	i %= 8;
    T_COM[i] = 0x01 << i;
    if(!L_flag && i == 1 && ui_status == 1)
        T_COM[i] = 0;
    else if(!L_flag && ui_status == 2 && (i==4||i==5||i==6||i==7))
        T_COM[i] = 0;
   
    smg_disp(t_display[data_disp[index][i]],T_COM[i]);
	i++;
}
